package com.student_system.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student_system.common.Result;
import com.student_system.system.entity.SysMenu;
import com.student_system.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    ISysMenuService sysMenuService;

    // 查询所有菜单
    @GetMapping("/getAllMenu")
    public Result.ResultData<List<SysMenu>> getAllMenu() {
        List<SysMenu> menus = sysMenuService.list();
        return Result.success(menus);
    }

    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/addMenu")
    public Result.ResultData<String> addMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.success("success addMenu");
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/deleteMenu/{menuId}")
    public Result.ResultData<String> deleteMenu(@PathVariable Integer menuId) {
        sysMenuService.removeById(menuId);
        return Result.success("success deleteMenu");
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @PutMapping("/updateMenu")
    public Result.ResultData<String> updateMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.success("success updateMenu");
    }

    /**
     * 分页查询菜单
     *
     *
     */
    @GetMapping("/getMenuByPage")
    public Page<SysMenu> pageMenu(@RequestParam(required = false) String menuName,
                                  @RequestParam(required = false) Integer pageSize,
                                  @RequestParam(required = false) Integer current) {
        if (current == null) {
            current = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page<SysMenu> page = new Page<>(current, pageSize);
        return sysMenuService.queryMenuPage(page, menuName);
    }


    /**
     * 查询菜单层级结构（树形结构）
     * 将父菜单ID为0的菜单作为根节点，父菜单ID为1的菜单作为二级菜单（演示层级关系）
     *
     * @return Map<String, Object> 返回格式：{ 父菜单ID : 菜单对象, ... }
     */
    @GetMapping("/getMenuTree")
    public Result.ResultData<List<Map<String, Object>>> getMenuTreeByUserId(@RequestParam("userId") String userId) {

        return Result.success(null);
    }
}
